<?php

/**
 * @author g.antoniadis (george@noodles.gr)
 * @copyright 2008
 */

class xarray
{
	
	public function getPosByKey(&$array, $key)
	{
		$x=0;
		foreach($array as $i=>$v) {
			if($key===$i)
			{
				return $x;
			}
			$x++;
		}
		return false;
	}
	
	public function getPosByVal(&$array, $val)
	{
		$x=0;
		foreach($array as $v) {
			if($val===$v)
			{
				return $x;
			}
			$x++;
		}
		return false;
	}
	
	public function getKey(&$array, $pos)
	{
		$x=0;
		foreach($array as $i=>$v) {
			if($pos==$x++) return $i;
		}
		return false;
	}
	
	public function moveKey(&$array, $key, $relativePosition)
	{
		if(!$relpos) return false;
		$from = me::getPosByKey($array,$key);
		
		if($from===false) return false;
		$to=$from+$relpos+($relpos>0?1:0);
		$len=count($array);
		if($to>=$len) {
			$val=$array[$key];
			unset($array[$key]);
			$array[$key]=$val;
		} else {
			if($to<0) $to=0;
			$new=array();       
			$x=0;
			foreach($array as  $i=>$v) {
				if($x++==$to) $new[$key]=$array[$key];
				if($i!==$key) $new[$i]=$v;
			}
			$array=$new;
		}
		return $array;
	}
	
	function inject($array, $position, $elements)
	{
		$left = array_slice ($array, 0, $position-1);
		$right = array_slice ($array, $position-1);
		$insert = explode ('\,', $elements);
		$array = array_merge ($left, $insert, $right);
		unset($left, $right, $insert);
		
		unset ($position, $elements);
		return $array;
	} 
}

?>